package com.lwx.sys.controller;


import com.lwx.sys.constant.Constant;
import com.lwx.sys.domain.Permission;
import com.lwx.sys.service.PermissionService;
import com.lwx.sys.utils.DataGridView;
import com.lwx.sys.utils.ResultObj;
import com.lwx.sys.vo.PermissionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 权限管理
 * @author lwx
 * @since 2020-04-04
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    /**
     * 加载所有的权限信息
     * @param permissionVo
     * @return
     */
    @GetMapping("/loadAllPermission")
    public DataGridView loadAllPermission(PermissionVo permissionVo){
        permissionVo.setType(Constant.TYPE_PERMISSION);
        return permissionService.loadAllPermission(permissionVo);
    }

    /**
     * 删除权限
     * @param id
     * @return
     */
    @DeleteMapping("/deletePermission")
    public ResultObj deletePermission(Integer id){
        try {
            permissionService.deletePermission(id);
            return ResultObj.DELETE_SUCESS;
        }catch (Exception e){
            return ResultObj.DELETE_ERROR;
        }
    }

    //批量删除

    @DeleteMapping("/batchDeletePermission")
    public ResultObj batchDeletePermission(Integer[] ids){
        try {
            permissionService.batchDeletePermission(ids);
            return ResultObj.DELETE_SUCESS;
        }catch (Exception e){
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 添加权限
     * @param permission
     * @return
     */
    @PostMapping("/addPermission")
    public ResultObj addPermission(Permission permission){
        try {
            permission.setType(Constant.TYPE_PERMISSION);
            permissionService.addPermission(permission);
            return ResultObj.ADD_SUCESS;
        }catch (Exception e){
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 更新权限
     * @param permission
     * @return
     */
    @PostMapping("/updatePermission")
    public ResultObj updatePermission(Permission permission){
        try {
            permissionService.updatePermission(permission);
            return ResultObj.ADD_SUCESS;
        }catch (Exception e){
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 根据id获取权限
     * @param id
     * @return
     */
    @GetMapping("/loadPermissionById")
    public DataGridView loadPermissionById(Integer id){
        return new DataGridView(permissionService.getById(id));
    }

    /**
     * 检查是否有子节点
     * @param id
     * @return
     */
    @GetMapping("/checkCurrentPermissionHasChildren")
    public DataGridView checkCurrentPermissionHasChildren(Integer id){
        Integer count = permissionService.queryPermissionCountByPid(id);
        return new DataGridView(count);
    }

    /**
     * 加载最大的排序号
     * @return
     */
    @GetMapping("/loadPermissionMaxOrderNum")
    public DataGridView loadPermissionMaxOrderNum(){
        Integer max = permissionService.queryPermissionMaxOrderNum();
        return new DataGridView(max+1);
    }
}

